package com.mc2.docapp.disabilityassessmenttool;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mc2.docapp.disabilityassessmenttool.HandComponent.C_Hand;
import com.mc2.docapp.disabilityassessmenttool.HandComponent.HC_CustDataType;
import com.mc2.docapp.disabilityassessmenttool.HandComponent.HC_InnerCalculations;
import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;
import com.mc2.docapp.disabilityassessmenttool.utils.MailerObject;
import com.mc2.docapp.disabilityassessmenttool.utils.SendMailTLS;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;


public class UpperLmbHandComponent extends AppCompatActivity {
    //this is act identifier
    private static final int id = Activities.UPPER_LIMB_ARM_COMPONENT;

    //creating new sub-component

    EditText lateralPinchLEFT;
    EditText lateralPinchRIGHT;
    EditText hookgraspLEFT;
    EditText hookgraspRIGHT;
    EditText opposnIndexFingerLEFT;
    EditText opposnIndexFingerRIGHT;
    EditText opposnMiddleFingerLEFT;
    EditText opposnMiddleFingerRIGHT;
    EditText opposnRIngFingerLEFT;
    EditText opposnRIngFingerRIGHT;
    EditText opposnLittleFingerLEFT;
    EditText opposnLittleFingerRIGHT;
    EditText cylindrGraspSmallLEFT;
    EditText cylindrGraspSmallRIGHT;
    EditText cylindrGraspLargeLEFT;
    EditText cylindrGraspLargeRIGHT;
    EditText spherGraspSmallLEFT;
    EditText spherGraspSmallRIGHT;
    EditText spherGraspLargeLEFT;
    EditText spherGraspLargeRIGHT;
    EditText gripLEFT;
    EditText gripRIGHT;
    EditText pinchLEFT;
    EditText pinchRIGHT;
    EditText thumbLEFT;
    EditText thumbRIGHT;
    EditText indexFingerLEFT;
    EditText indexFingerRIGHT;
    EditText middleFingerLEFT;
    EditText middleFingerRIGHT;
    EditText ringFingerLEFT;
    EditText ringFingerRIGHT;
    EditText littleFingerLEFT;
    EditText littleFingerRIGHT;

    //db instance
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upper_lmb_arm_component);
        Button saveNnextButton = findViewById(R.id.saveAndNext);
        lateralPinchLEFT = (EditText) findViewById(R.id.lateralPinchLEFT);
        lateralPinchRIGHT = (EditText) findViewById(R.id.lateralPinchRIGHT);
        hookgraspLEFT = (EditText) findViewById(R.id.hookgraspLEFT);
        hookgraspRIGHT = (EditText) findViewById(R.id.hookgraspRIGHT);
        opposnIndexFingerLEFT = (EditText) findViewById(R.id.opposnIndexFingerLEFT);
        opposnIndexFingerRIGHT = (EditText) findViewById(R.id.opposnIndexFingerRIGHT);
        opposnMiddleFingerLEFT = (EditText) findViewById(R.id.opposnMiddleFingerLEFT);
        opposnMiddleFingerRIGHT = (EditText) findViewById(R.id.opposnMiddleFingerRIGHT);
        opposnRIngFingerLEFT = (EditText) findViewById(R.id.opposnRIngFingerLEFT);
        opposnRIngFingerRIGHT = (EditText) findViewById(R.id.opposnRIngFingerRIGHT);
        opposnLittleFingerLEFT = (EditText) findViewById(R.id.opposnLittleFingerLEFT);
        opposnLittleFingerRIGHT = (EditText) findViewById(R.id.opposnLittleFingerRIGHT);
        cylindrGraspSmallLEFT = (EditText) findViewById(R.id.cylindrGraspSmallLEFT);
        cylindrGraspSmallRIGHT = (EditText) findViewById(R.id.cylindrGraspSmallRIGHT);
        cylindrGraspLargeLEFT = (EditText) findViewById(R.id.cylindrGraspLargeLEFT);
        cylindrGraspLargeRIGHT = (EditText) findViewById(R.id.cylindrGraspLargeRIGHT);
        spherGraspSmallLEFT = (EditText) findViewById(R.id.spherGraspSmallLEFT);
        spherGraspSmallRIGHT = (EditText) findViewById(R.id.spherGraspSmallRIGHT);
        spherGraspLargeLEFT = (EditText) findViewById(R.id.spherGraspLargeLEFT);
        spherGraspLargeRIGHT = (EditText) findViewById(R.id.spherGraspLargeRIGHT);
        gripLEFT = (EditText) findViewById(R.id.gripLEFT);
        gripRIGHT = (EditText) findViewById(R.id.gripRIGHT);
        pinchLEFT = (EditText) findViewById(R.id.pinchLEFT);
        pinchRIGHT = (EditText) findViewById(R.id.pinchRIGHT);
        thumbLEFT = (EditText) findViewById(R.id.thumbLEFT);
        thumbRIGHT = (EditText) findViewById(R.id.thumbRIGHT);
        indexFingerLEFT = (EditText) findViewById(R.id.indexFingerLEFT);
        indexFingerRIGHT = (EditText) findViewById(R.id.indexFingerRIGHT);
        middleFingerLEFT = (EditText) findViewById(R.id.middleFingerLEFT);
        middleFingerRIGHT = (EditText) findViewById(R.id.middleFingerRIGHT);
        ringFingerLEFT = (EditText) findViewById(R.id.ringFingerLEFT);
        ringFingerRIGHT = (EditText) findViewById(R.id.ringFingerRIGHT);
        littleFingerLEFT = (EditText) findViewById(R.id.littleFingerLEFT);
        littleFingerRIGHT = (EditText) findViewById(R.id.littleFingerRIGHT);

        initRealmDB();
        sanitizeInputs();
        try {
            //mapUI((realm.where(C_Hand.class)/*.equalTo("id", id)*/.findAll()).get(0));
            System.out.println();
        } catch (Exception e) {
            System.out.println(e.getCause() + " " + e.getMessage());
        }

        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                C_Hand c_Hand = new C_Hand();
                c_Hand.setLateral_pinch(new HC_CustDataType(Integer.parseInt(IfNull(lateralPinchRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(lateralPinchLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setHook_grasp(new HC_CustDataType(Integer.parseInt(IfNull(hookgraspRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(hookgraspLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setOpposition_to_index_finger(new HC_CustDataType(Integer.parseInt(IfNull(opposnIndexFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(opposnIndexFingerLEFT.getText().toString(), "0")), 0, 0, 0));

                c_Hand.setOpposition_to_middle_finger(new HC_CustDataType(Integer.parseInt(IfNull(opposnMiddleFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(opposnMiddleFingerLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setOpposition_to_ring_finger(new HC_CustDataType(Integer.parseInt(IfNull(opposnRIngFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(opposnIndexFingerLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setOpposition_to_little_finger(new HC_CustDataType(Integer.parseInt(IfNull(opposnLittleFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(opposnLittleFingerLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setCylindrical_grasp_Small_1(new HC_CustDataType(Integer.parseInt(IfNull(cylindrGraspSmallRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(cylindrGraspSmallLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setCylindrical_grasp_Large_4(new HC_CustDataType(Integer.parseInt(IfNull(cylindrGraspLargeRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(cylindrGraspLargeLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setSpherical_grasp_Small_1(new HC_CustDataType(Integer.parseInt(IfNull(spherGraspSmallRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(spherGraspSmallLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setSpherical_grasp_Large_4(new HC_CustDataType(Integer.parseInt(IfNull(spherGraspLargeRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(spherGraspLargeLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setGrip(new HC_CustDataType(Integer.parseInt(IfNull(gripRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(gripLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setPinch(new HC_CustDataType(Integer.parseInt(IfNull(pinchRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(pinchLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setThumb(new HC_CustDataType(Integer.parseInt(IfNull(thumbRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(thumbLEFT.getText().toString(), "0")), 0d, 0, 0));

                c_Hand.setIndex_finger(new HC_CustDataType(Integer.parseInt(IfNull(indexFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(indexFingerLEFT.getText().toString(), "0")), 6d, 0, 0));

                c_Hand.setMiddle_finger(new HC_CustDataType(Integer.parseInt(IfNull(middleFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(middleFingerLEFT.getText().toString(), "0")), 5d, 0, 0));

                c_Hand.setRing_finger(new HC_CustDataType(Integer.parseInt(IfNull(ringFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(ringFingerLEFT.getText().toString(), "0")), 5d, 0, 0));

                c_Hand.setLittle_finger(new HC_CustDataType(Integer.parseInt(IfNull(littleFingerRIGHT.getText().toString(), "0")),
                        Integer.parseInt(IfNull(littleFingerLEFT.getText().toString(), "0")), 0d, 0, 0));


                HC_InnerCalculations calc = new HC_InnerCalculations();

                double rightSum = c_Hand.getLateral_pinch().getActual_right() +
                        c_Hand.getHook_grasp().getActual_right() +
                        c_Hand.getOpposition_to_index_finger().getActual_right() +
                        c_Hand.getOpposition_to_middle_finger().getActual_right() +
                        c_Hand.getOpposition_to_ring_finger().getActual_right() +
                        c_Hand.getOpposition_to_little_finger().getActual_right() +
                        c_Hand.getCylindrical_grasp_Small_1().getActual_right() +
                        c_Hand.getCylindrical_grasp_Large_4().getActual_right() +
                        c_Hand.getSpherical_grasp_Small_1().getActual_right() +
                        c_Hand.getSpherical_grasp_Large_4().getActual_right() +
                        c_Hand.getGrip().getActual_right() +
                        c_Hand.getPinch().getActual_right() +
                        c_Hand.getThumb().getActual_right() +
                        c_Hand.getIndex_finger().getActual_right() +
                        c_Hand.getMiddle_finger().getActual_right() +
                        c_Hand.getRing_finger().getActual_right() +
                        c_Hand.getLittle_finger().getActual_right() + 0d;
                if (rightSum > 90d) {
                    rightSum = 90d;
                }
                calc.setSummation_right(rightSum);

                double leftSum = c_Hand.getLateral_pinch().getActual_left() +
                        c_Hand.getHook_grasp().getActual_left() +
                        c_Hand.getOpposition_to_index_finger().getActual_left() +
                        c_Hand.getOpposition_to_middle_finger().getActual_left() +
                        c_Hand.getOpposition_to_ring_finger().getActual_left() +
                        c_Hand.getOpposition_to_little_finger().getActual_left() +
                        c_Hand.getCylindrical_grasp_Small_1().getActual_left() +
                        c_Hand.getCylindrical_grasp_Large_4().getActual_left() +
                        c_Hand.getSpherical_grasp_Small_1().getActual_left() +
                        c_Hand.getSpherical_grasp_Large_4().getActual_left() +
                        c_Hand.getGrip().getActual_left() +
                        c_Hand.getPinch().getActual_left() +
                        c_Hand.getThumb().getActual_left() +
                        c_Hand.getIndex_finger().getActual_left() +
                        c_Hand.getMiddle_finger().getActual_left() +
                        c_Hand.getRing_finger().getActual_left() +
                        c_Hand.getLittle_finger().getActual_left() + 0d;
                if (leftSum > 90d) {
                    leftSum = 90d;
                }
                calc.setSummation_left(leftSum);
                c_Hand.setHc_innerCalculations(calc);

                NewPatient.finalDto.getClass_upperLimb_component().setClass_hand_component(c_Hand);

                calArmHandComp(true);
                calArmHandComp(false);


                realm.beginTransaction();
                realm.delete(C_Hand.class);
                realm.insert(c_Hand);
                realm.commitTransaction();
                try {
                    String str = new ObjectMapper().writeValueAsString(c_Hand).toString();
//                    SendMailTLS.getMailer().sendMail(new MailerObject("saurab.hbhumkar@gmail.com", "MC2 Docapp", "str"));
                } catch (Exception e) {
                    System.out.println();
                }
                Intent intent = new Intent(UpperLmbHandComponent.this, router(UpperLmbHandComponent.class));
                startActivity(intent);
            }
        });
        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });
    }

    private void mapUI(C_Hand oldData) {

        lateralPinchLEFT.setText(String.valueOf((long) (oldData.getLateral_pinch().getActual_left())));
        lateralPinchRIGHT.setText(String.valueOf((long) (oldData.getLateral_pinch().getActual_right())));

        hookgraspLEFT.setText(String.valueOf((long) (oldData.getHook_grasp().getActual_left())));
        hookgraspRIGHT.setText(String.valueOf((long) (oldData.getHook_grasp().getActual_right())));

        opposnIndexFingerLEFT.setText(String.valueOf((long) (oldData.getOpposition_to_index_finger().getActual_left())));
        opposnIndexFingerRIGHT.setText(String.valueOf((long) (oldData.getOpposition_to_index_finger().getActual_right())));

        opposnMiddleFingerLEFT.setText(String.valueOf((long) (oldData.getOpposition_to_middle_finger().getActual_left())));
        opposnMiddleFingerRIGHT.setText(String.valueOf((long) (oldData.getOpposition_to_middle_finger().getActual_right())));

        opposnRIngFingerLEFT.setText(String.valueOf((long) (oldData.getOpposition_to_ring_finger().getActual_left())));
        opposnRIngFingerRIGHT.setText(String.valueOf((long) (oldData.getOpposition_to_ring_finger().getActual_right())));

        opposnLittleFingerLEFT.setText(String.valueOf((long) (oldData.getOpposition_to_little_finger().getActual_left())));
        opposnLittleFingerRIGHT.setText(String.valueOf((long) (oldData.getOpposition_to_little_finger().getActual_right())));


        cylindrGraspSmallLEFT.setText(String.valueOf((long) (oldData.getCylindrical_grasp_Small_1().getActual_left())));
        cylindrGraspSmallRIGHT.setText(String.valueOf((long) (oldData.getCylindrical_grasp_Small_1().getActual_right())));

        cylindrGraspLargeLEFT.setText(String.valueOf((long) (oldData.getCylindrical_grasp_Large_4().getActual_left())));
        cylindrGraspLargeRIGHT.setText(String.valueOf((long) (oldData.getCylindrical_grasp_Large_4().getActual_right())));

        spherGraspSmallLEFT.setText(String.valueOf((long) (oldData.getSpherical_grasp_Small_1().getActual_left())));
        spherGraspSmallRIGHT.setText(String.valueOf((long) (oldData.getSpherical_grasp_Small_1().getActual_right())));

        spherGraspLargeLEFT.setText(String.valueOf((long) (oldData.getSpherical_grasp_Large_4().getActual_left())));
        spherGraspLargeRIGHT.setText(String.valueOf((long) (oldData.getSpherical_grasp_Large_4().getActual_right())));

        gripLEFT.setText(String.valueOf((long) (oldData.getGrip().getActual_left())));
        gripRIGHT.setText(String.valueOf((long) (oldData.getGrip().getActual_right())));

        pinchLEFT.setText(String.valueOf((long) (oldData.getPinch().getActual_left())));
        pinchRIGHT.setText(String.valueOf((long) (oldData.getPinch().getActual_right())));

        thumbLEFT.setText(String.valueOf((long) (oldData.getThumb().getActual_left())));
        thumbRIGHT.setText(String.valueOf((long) (oldData.getThumb().getActual_right())));

        indexFingerLEFT.setText(String.valueOf((long) (oldData.getIndex_finger().getActual_left())));
        indexFingerRIGHT.setText(String.valueOf((long) (oldData.getIndex_finger().getActual_right())));

        middleFingerLEFT.setText(String.valueOf((long) (oldData.getMiddle_finger().getActual_left())));
        middleFingerRIGHT.setText(String.valueOf((long) (oldData.getMiddle_finger().getActual_right())));

        ringFingerLEFT.setText(String.valueOf((long) (oldData.getRing_finger().getActual_left())));
        ringFingerRIGHT.setText(String.valueOf((long) (oldData.getRing_finger().getActual_right())));

        littleFingerLEFT.setText(String.valueOf((long) (oldData.getLittle_finger().getActual_left())));
        littleFingerRIGHT.setText(String.valueOf((long) (oldData.getLittle_finger().getActual_right())));

    }

    public String IfNull(String value, String defaultValue) {
        if (value == null || value.isEmpty()) {
            return defaultValue;
        } else {
            return value;
        }
    }

    private void calArmHandComp(Boolean isLeft) {
        if (isLeft) {
            double totArmComp = NewPatient.finalDto.getClass_upperLimb_component().getClass_arm_component().getTotalArmCommponentLeft();
            double totHandComm = NewPatient.finalDto.getClass_upperLimb_component().getClass_hand_component().getHc_innerCalculations().getSummation_left();

            double step1;

            step1 = calByFormula(totArmComp, totHandComm);
            NewPatient.finalDto.getClass_upperLimb_component().setArmPlushandCompLeft(step1);

        } else {
            double totArmComp = NewPatient.finalDto.getClass_upperLimb_component().getClass_arm_component().getTotalArmCommponentRight();
            double totHandComm = NewPatient.finalDto.getClass_upperLimb_component().getClass_hand_component().getHc_innerCalculations().getSummation_right();

            double step1;

            step1 = calByFormula(totArmComp, totHandComm);
            NewPatient.finalDto.getClass_upperLimb_component().setArmPlushandCompRight(step1);


        }
    }

    private double calByFormula(double a, double b) {
        if (a < b) {
            a = a + b;
            b = a - b;
            a = a - b;
        }
        double c = a + (((90 - a) * b) / 90.0);
        return c;
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(id + ".realm")
                .build();
        realm = Realm.getInstance(config);
    }


    public void sanitizeInputs() {
        lateralPinchLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        lateralPinchRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        hookgraspLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hookgraspRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        opposnIndexFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});
        opposnIndexFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});

        opposnMiddleFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});
        opposnMiddleFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});

        opposnRIngFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});
        opposnRIngFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});

        opposnLittleFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});
        opposnLittleFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "2")});

        cylindrGraspSmallLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});
        cylindrGraspSmallRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});
        cylindrGraspLargeLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});
        cylindrGraspLargeRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});
        spherGraspSmallLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});
        spherGraspLargeLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});
        spherGraspLargeRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "3")});

        gripLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "20")});
        gripRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "20")});

        pinchLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "10")});
        pinchRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "10")});

        thumbLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "9")});
        thumbRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "9")});

        indexFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "6")});
        indexFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "6")});

        middleFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        middleFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        ringFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        ringFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        littleFingerLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        littleFingerRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
    }
}
